package com.dofast.bt.base;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.dofast.bt.account.Account;
import com.dofast.bt.account.AccountManager;
import com.dofast.bt.dao.DBManager;
import com.dofast.bt.util.SpfUtil;
import com.jiongbull.jlog.Logger;
import com.jiongbull.jlog.constant.LogLevel;
import com.jiongbull.jlog.constant.LogSegment;
import com.jiongbull.jlog.util.TimeUtils;

import org.xutils.DbManager;
import org.xutils.db.table.TableEntity;
import org.xutils.x;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * username:Created by hsj on 2016/10/11 0011.
 * class：
 * 类描述：应用入口程序
 */

public class BaseApplication extends Application {
    public Context context;
    public static BaseApplication mInstance;
    private Account mAccount = null;
    private static Logger sLogger;
    private static  DbManager db;
    private DBManager dbHelper;
    public static BaseApplication getInstance() {
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.print(new Date().getTime());
        context = getApplicationContext();
        mInstance = this;
        x.Ext.init(this);
        initLog();
        //initDBA();
        //导入数据库
        dbHelper = new DBManager(this);
        dbHelper.openDatabase();
        System.out.print(new Date().getTime());
    }

    public void initDBA() {
        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig()
                //设置数据库名，默认xutils.db
                .setDbName("teaShop")
                //设置表创建的监听
                .setTableCreateListener(new DbManager.TableCreateListener() {
                    @Override
                    public void onTableCreated(DbManager db, TableEntity<?> table) {
                        Log.e("DB------", "onTableCreated：" + table.getName());
                    }
                })
                //设置是否允许事务，默认true
                //.setAllowTransaction(true)
                //设置数据库路径，默认安装程序路径下
                //.setDbDir(new File("/mnt/sdcard/Tea"))
                //设置数据库的版本号
                //.setDbVersion(1)
                //设置数据库更新的监听
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion,
                                          int newVersion) {
                    }
                })
                //设置数据库打开的监听
                .setDbOpenListener(new DbManager.DbOpenListener() {
                    @Override
                    public void onDbOpened(DbManager db) {
                        //开启数据库支持多线程操作，提升性能
                        db.getDatabase().enableWriteAheadLogging();
                    }
                });
        db = x.getDb(daoConfig);
    }

    /**
     * 自定义log
     */
    private void initLog() {
        //自定义log
        List<String> logLevels = new ArrayList<>();
        logLevels.add(LogLevel.ERROR);
//        logLevels.add(LogLevel.WTF);
        logLevels.add(LogLevel.WARN);
        logLevels.add(LogLevel.DEBUG);
        logLevels.add(LogLevel.INFO);
        logLevels.add(LogLevel.VERBOSE);
        logLevels.add(LogLevel.JSON);


        sLogger = Logger.Builder.newBuilder(getApplicationContext(), "mylog")
                /* properties below are default value, you can modify them or not. */
                .setDebug(true)
                .setWriteToFile(false)
                .setLogDir("mylog")
                .setLogPrefix("")
                .setLogSegment(LogSegment.TWELVE_HOURS)
                .setLogLevelsForFile(logLevels)
                .setZoneOffset(TimeUtils.ZoneOffset.P0800)
                .setTimeFormat("yyyy-MM-dd HH:mm:ss")
                .setPackagedLevel(0)
                .setStorage(null)
                .build();
    }

    public Account getAccount() {
        String uid = SpfUtil.getInstance().get(AccountManager.SP_KEY_ACTIVE_ID);
        if (uid != null && !TextUtils.isEmpty(uid)) {
            mAccount = AccountManager.getInstance().getAccount(uid);
        }
        return mAccount;
    }


    public static Logger getLogger() {
        return sLogger;

    }
    public static DbManager getDbManager(){
        return db;
    }
}
